package kr.co.mustgo.model.dao.qna;

import java.util.HashMap;
import java.util.List;

import kr.co.mustgo.domain.qna.QnABoard;
import kr.co.mustgo.util.BoardUtilities;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class QnABoardDAOImpl implements QnABoardDAO{

	@Autowired
	private SqlSessionTemplate session;
	
	@Override
	public int insertContentForNewCount(QnABoard qnaBoard) {
		return session.insert(makeSqlId("insertContentForNewCount"), qnaBoard);
	}

	@Override
	public int insertContentForReply(QnABoard qnaBoard) {
		return session.insert(makeSqlId("insertContentForReply"), qnaBoard);
	}

	@Override
	public int updateContent(QnABoard qnaBoard) {
		return session.update(makeSqlId("updateContent"), qnaBoard);
	}

	@Override
	public int updateViewCount(int code) {
		return session.update(makeSqlId("updateViewCount"), code);
	}
	
	@Override
	public QnABoard selectContentByCode(int code) {
		return session.selectOne(makeSqlId("selectContentByCode"), code);
	}
	
	@Override
	public List<QnABoard> selectQnABoardList(int page){
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		return session.selectList(makeSqlId("selectQnABoardList"), map);
	}
	
	@Override
	public int selectTotalContent() {
		return session.selectOne(makeSqlId("selectTotalContent"));
	}
	

	@Override
	public int deleteContentByCode(int code) {
		return session.delete(makeSqlId("deleteContentByCode"), code);
	}

	@Override
	public int updateRestep(int refamily, int restep) {
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("refamily", refamily);
		map.put("restep", restep);
		return session.update(makeSqlId("updateRestep"), map);
	}

	@Override
	public List<QnABoard> selectQnABoardListCategory(int page, String category) {
		HashMap map = new HashMap();
		map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
		map.put("page", page);
		map.put("category", category);
		return session.selectList(makeSqlId("selectQnABoardListCategory"), map);
	}
	
	@Override
	public int selectCategoryContent(String category) {
		return session.selectOne(makeSqlId("selectCategoryContent"), category);
	}
	

	@Override
	public List<QnABoard> selectQnABoardSearch(int page, String search, String keyword){
			HashMap map = new HashMap();
			
	map.put("contentPerPage", BoardUtilities.CONTENT_PER_PAGE);
	map.put("page", page);
	map.put("search", search);
	map.put("keyword", keyword);
	return session.selectList(makeSqlId("selectQnABoardSearch"), map);
	}


	@Override
	public int selectSearchContent(String search, String keyword) {
		HashMap map = new HashMap();
		map.put("search", search);
		map.put("keyword", keyword);
		return session.selectOne(makeSqlId("selectSearchContent"), map);
	}

	private String makeSqlId(String id){
		return "qnaBoard."+id;
	}
}
